<template>
  <div class="good-item">
    <div style="">
      <div class="good-img">
        <a>
          <img style="padding-top: 20px" :src="item.pic" :alt="item.name" :key="item.id">
        </a>
      </div>
      <h3 style="color: #1890ff">{{ item.brandName }}</h3>
      <h6 class="good-title">{{ item.name }}</h6>
      <div class="good-price pr">
        <div class="ds pa">
          <a>
            <m-button text="查看详情" @click="toDetail(item)" style="margin: 0 5px"></m-button>
          </a>
          <m-button @click="addCart(item)" text="加入购物车" style="margin: 0 5px" classStyle="main-btn"/>
        </div>
        <p><span style="font-size:14px">￥</span>{{ Number(item.price).toFixed(2) }}</p>
      </div>
    </div>
  </div>
</template>
<script>
import MButton from './MButton.vue'
import cartItemApi from '../api/cartItem'
import { useStore } from 'vuex'
import { getToken } from '../utils/auth'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import productHistoryApi from '../api/productHistory'

export default {
  components: { MButton },
  props: {
    // 商品信息
    item: {
      type: Object,
      default () {
        return {}
      }
    },
  },
  setup () {
    const store = useStore()
    const router = useRouter()
    // 加入购物车
    const addCart = async (item) => {
      let tempItem = {
        id: item.id,
        name: item.name,
        pic: item.pic,
        price: item.price,
        point: item.point
      }
      // 登录了 会直接存在 会员下  没有登录会存在vuex
      if (getToken()) {
        // console.log('打印的正常:product:', JSON.stringify(item))
        const res = await cartItemApi.saveCart({ productId: tempItem.id })
        if (res.code === 200) {
          ElMessage.success(`'${tempItem.name}'加入购物车成功`)
          store.commit('ADD_CART', tempItem)
        }
      } else {
        // 未登录
        store.commit('ADD_CART', tempItem)
        ElMessage.success(`'${tempItem.name}'加入购物车成功`)
      }
    }

    // 去详情页面
    const toDetail = (item) => {
      if (getToken()) {
        productHistoryApi.save(item.id).then(res => {
          router.push({
            path: '/goods/detail',
            query: {
              id: item.id
            }
          })
        })
      } else {
        router.push({
          path: '/goods/detail',
          query: {
            id: item.id
          }
        })
      }

    }
    return {
      addCart,
      toDetail
    }
  }
}
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
@import "../assets/style/mixin";
@import "../assets/style/theme";

.good-item {
  background: #fff;
  width: 220px;
  transition: all .5s;
  height: 360px;

  &:hover {
    transform: translateY(-3px);
    box-shadow: 1px 1px 20px #999;

    .good-price p {
      display: none;
    }

    .ds {
      display: flex;
    }
  }

  .ds {
    @extend %block-center;
    width: 100%;
    display: none;
  }

  .good-img {
    img {
      margin: 10px auto 10px;
      @include wh(180px);
      display: block;
    }
  }

  .good-price {
    margin: 15px 0;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: #d44d44;
    font-family: Arial;
    font-size: 18px;
    font-weight: 700;
  }

  .good-title {
    line-height: 1.2;
    font-size: 16px;
    color: #424242;
    margin: 0 auto;
    padding: 0 14px;
    text-align: center;
    overflow: hidden;
  }

  h3 {
    text-align: center;
    line-height: 1.2;
    font-size: 12px;
    color: #d0d0d0;
    padding: 10px;
  }

}
</style>
